$(document).ready(function() {

	var urlParams = self.location.pathname.split("/");
	var urlSection = urlParams[2];
	var urlAction = urlParams[3];
	var urlId = urlParams[4];

	if (urlSection == "blogcomments" && urlAction == "list") {
		showBlogComments(0);
	}

	var currentSection = $("#currentSection").val();
	if (currentSection == "content") {
		sectionSubmit();
	}

	$("#section").change(function() {
		sectionSubmit();
	});

	$("#contentSaveButton").click(function() {
		// Recogemos los valores del formulario
		var section = $.trim($("#section").val());
		var block = $.trim($("#block").val());
		var language = $.trim($("#language").val());
		var visible = $.trim($("#visible").val());
		var html = $.trim($("#html").val());
		// Llamamos al script
		$.post($("#action").val(),
			{section: section, block: block, language: language, visible: visible, html: html},
			function(data) {
				alert(data); return false;
				/*
				var result = data.substr(1,2);
				var message = data.substr(4);
				if (result == "ko") {
					showFancybox('<div class="fancyboxErrors">'+message+'</div>', false);
					return false;
				} else {
					// Mostramos un mensaje
					showFancybox('<div class="fancyboxNotifications">'+message+'</div>', true);
					return false;
				}
				*/
			}
		);
	});

	$("#saveButton").click(function() {
		// Ocultamos las cajas de mensajes
		$("#error-box").hide();
		$("#notification-box").hide();
		// Llamamos al script
		$.post($("#action").val(), $("form").serialize(), function(data) {
			var result = data.substr(0,2);
			var message = data.substr(3);
			if (result == "ko") {
				$("#error-box").html(message);
				$("#error-box").show();
				return false;
			} else {
				$("#notification-box").html(message);
				$("#notification-box").show();
				return false;
			}
		});
	});

	$("#validateCommentButton").click(function() {
		$("#operation").val("validate");
		// Ocultamos las cajas de mensajes
		$("#error-box").hide();
		$("#notification-box").hide();
		// Llamamos al script
		$.post($("#action").val(), $("form").serialize(), function(data) {
			var result = data.substr(0,2);
			var message = data.substr(3);
			if (result == "ko") {
				$("#error-box").html(message);
				$("#error-box").show();
				return false;
			} else {
				$("#notification-box").html(message);
				$("#notification-box").show();
				return false;
			}
		});
	});

	$("#blockCommentButton").click(function() {
		$("#operation").val("block");
		// Ocultamos las cajas de mensajes
		$("#error-box").hide();
		$("#notification-box").hide();
		// Llamamos al script
		$.post($("#action").val(), $("form").serialize(), function(data) {
			var result = data.substr(0,2);
			var message = data.substr(3);
			if (result == "ko") {
				$("#error-box").html(message);
				$("#error-box").show();
				return false;
			} else {
				$("#notification-box").html(message);
				$("#notification-box").show();
				return false;
			}
		});
	});

	$("#showValidBlogCommentsButton").click(function() {
		showBlogComments(1);
	});

	$("#showBlockedBlogCommentsButton").click(function() {
		showBlogComments(2);
	});

	$("#showNewBlogCommentsButton").click(function() {
		showBlogComments(0);
	});

	$("a.thumb").fancybox();

});

function showBlogComments(status) {
	var entryId = $("#entryId").val();
	$.post($("#showBlogCommentsUrl").val(),
		{status: status, entryId: entryId},
		function(data) {
			$("#contentTable").html(data);
		}
	);
}

function sectionSubmit() {
	var section = $("#section").val();
	$.post($("#action").val(),
		{section: section},
		function(data) {
			$("#contentTable").html(data);
		}
	);
}

$(function() {
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/admin/js/tiny_mce/tiny_mce.js',
		// General options
		theme : "advanced",
		skin : "o2k7",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1 : "formatselect,forecolor,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,sup,sub,|,bullist,numlist,|,outdent,indent,|,link,unlink,charmap,|,insertdate,inserttime,|,code,|",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		// Example content CSS (should be your site CSS)
		content_css : "",
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"
	});
	/*
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/admin/js/tiny_mce/tiny_mce.js',
		// General options
		theme : "advanced",
		skin : "o2k7",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,styleselect,formatselect,fontselect,fontsizeselect,forecolor,backcolor",
		theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,|,sup,sub,|,bullist,numlist,|,outdent,indent,|,link,unlink,anchor,image,media,charmap,|,insertdate,inserttime",
		theme_advanced_buttons3 : "tablecontrols,|,styleprops,|,code,cleanup,preview",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		// Example content CSS (should be your site CSS)
		content_css : "/css/paragraph.css",
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"
	});
	*/
});
